package com.zisuyi.uilibrary.module.activty.invite

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.weilele.mvvm.base.helper.LiveDataWrap
import com.weilele.mvvm.base.helper.StatusValue
import com.weilele.mvvm.base.helper.annotation.MvvmViewModel
import com.weilele.mvvm.base.helper.appCompatActivity
import com.weilele.mvvm.base.helper.wrapObserver
import com.weilele.mvvm.utils.activity.*
import com.zisuyi.game.sdk.net.bean.InviteBean
import com.zisuyi.uilibrary.R
import com.zisuyi.game.sdk.utils.UserData
import com.zisuyi.uilibrary.base.BaseUiActivity
import com.zisuyi.uilibrary.databinding.ActivityInviteBinding
import com.zisuyi.uilibrary.module.dialog.ShareDialog
import com.zisuyi.uilibrary.uitils.copyToClipboard


class InviteActivity : BaseUiActivity<ActivityInviteBinding>() {
    companion object {
        fun start(appCompatActivity: AppCompatActivity?) {
            appCompatActivity.navigateTo<InviteActivity>()
        }
    }

    @MvvmViewModel
    private lateinit var inviteViewModel: InviteViewModel


    override fun getTitleBarTitle(): CharSequence? {
        return getResString(R.string.invite_friends)
    }

    override fun getClickView(): List<View?>? {
        return listOf(mBinding.btCopyLink, mBinding.btCopyCode, mBinding.etCodeGet, mBinding.tvShare)
    }

    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        UserData.userInfo?.let {
            mBinding.tvLink.textFrom(it.shareLink)
            mBinding.tvCode.textFrom(it.code ?: it.shareLink.split("/").lastOrNull())
            mBinding.tvHint.text = String.format(
                    getResString(R.string.you_will_get_800_coins_you_friend_will_get_300_coins_with_your_invitation_code).toString(),
                    it.inviterCoin, it.inviteeCoin
            )
            if (!it.invitedCode.isNullOrBlank()) {
                hideInputCodeView()
            }
        }
    }

    override fun onSingleClick(view: View) {
        super.onSingleClick(view)
        when (view) {
            mBinding.btCopyCode -> {
                copyToClipboard(mBinding.tvCode.text.toString())
            }
            mBinding.btCopyLink -> {
                copyToClipboard(mBinding.tvLink.text.toString())
            }
            mBinding.etCodeGet -> {
                val code = mBinding.etCode.text.toString()
                if (code == mBinding.tvCode.text.toString()) {
                    "Can’t enter your own invitation code".toast()
                } else {
                    inviteViewModel.inviteCode(code)
                }
            }
            mBinding.tvShare -> {
                ShareDialog.show(appCompatActivity)
            }
        }
    }

    override fun getObserverLiveData(): List<LiveDataWrap>? {
        return listOf(inviteViewModel.inviteData wrapObserver ::onInviteCode)
    }

    private fun onInviteCode(value: StatusValue<InviteBean>?) {
        when (value) {
            is StatusValue.Running -> {
                showLoadingView()
                appCompatActivity.hiddenKeyboard()
            }
            is StatusValue.Error -> {
                showContentView()
            }
            is StatusValue.Success -> {
                showContentView()
                "${value.nonNullValue.addCoin} diamond have been added to your account".toast(Toast.LENGTH_LONG)
                UserData.userInfo?.invitedCode = mBinding.etCode.text.toString()
                hideInputCodeView()
            }
        }
    }

    private fun hideInputCodeView() {
        mBinding.llCodeHint.gone()
        mBinding.tvCodeHint.gone()
    }
}